from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from .project import Project
from lib.django import models as mymodels

from .lib import ProjectSubMixin

class Contract(ProjectSubMixin, models.Model):
    project = models.ForeignKey(
        verbose_name=_('项目'), 
        to=Project, 
        on_delete=models.CASCADE
    )

    contract_name = models.CharField(
        verbose_name=_('合同名称'), 
        max_length=255, 
        default=''
    )
    
    contract_time = models.DateField(
        verbose_name=_('主合同签订时间'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    date_of_submission_contract_by_sponsor = models.DateField(
        verbose_name=_('申办方提交合同日期'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    contract_agreement_date = models.DateField(
        verbose_name=_('合同协商一致日期'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    date_of_submission_sealed_contract_by_sponsor = models.DateField(
        verbose_name=_('申办方提交盖章版合同日期'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    clinical_trial_institution_contract_seal_date = models.DateField(
        verbose_name=_('临床试验机构合同盖章日期'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    contract_amount = models.DecimalField(
        verbose_name=_('主合同签订金额'), 
        max_digits=10, 
        decimal_places=2
    )
    
    management_fee = models.TextField(
        verbose_name=_('机构管理费到位'), 
        blank=True, 
        default=''
    )
    
    funds_appropriation = models.TextField(
        verbose_name=_('合同款项拨付'),
        help_text=_('注明分几次拨付及比例'),
        blank=True,
        default=''
    )
    
    is_first_management_fee_available = mymodels.YesNoField(
        verbose_name=_('首次拨款机构管理费是否到位'), 
        default=False
    )

    class Meta:
        default_permissions = []
        db_table = 'project_contract'
        verbose_name = _('合同')
        verbose_name_plural = verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:contract/detail", kwargs={'project_pk': project_id, 'pk': id})
